package com.huarui.sale.action;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.ewaf.framework.action.BaseAction;
import org.ewaf.framework.model.ListModel;

import com.huarui.part.model.PartPropertySearch;
import com.huarui.sale.service.SaleService;
import com.huarui.sale.util.SaleConstantEnum;
import com.huarui.saleReturn.util.ConstantParamEnum;
import com.huarui.util.JsonUtil;

public class SaleQueryAction extends BaseAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8393843246652099911L;
	private Map<String, Object> jsonObject;
	private String queryParam;
	@SuppressWarnings("unchecked")
	private ListModel saleListModel;
	private SaleService saleService;

	// 分页
	private Integer start;
	private Integer limit;
	private String sort;
	private String dir;	
	
	private String searchJson;
	/**
	 * 销售单浏览
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String fillSaleBrowser(){
		jsonObject = new HashMap<String,Object>();
		if (queryParam != null) {
			JSONObject queryObject = JSONObject.fromObject(queryParam);
			jsonObject = (Map) JSONObject.toBean(queryObject, Map.class);
		}
		saleListModel = ListModelParamUtil();
		ListModel sales = saleService.getSaleListModelByConditions(jsonObject,saleListModel);
		jsonObject.clear();
		jsonObject.put(ConstantParamEnum.TOTALPROPERTY.getType(), sales.getTotal());
		jsonObject.put(ConstantParamEnum.ROOT.getType(), sales.getResult());
		return SUCCESS;
	}
	
	
	@SuppressWarnings("unchecked")
	public String fillSaleItemBrowser(){
		jsonObject = new HashMap();
		if(queryParam != null){
			JSONObject queryObject = JSONObject.fromObject(queryParam);
			jsonObject = (Map) JSONObject.toBean(queryObject, Map.class);	
		}
		if(searchJson!=null){
			PartPropertySearch partPropertySearch=JsonUtil.getBean(searchJson, PartPropertySearch.class);
			jsonObject.put("searchJson", partPropertySearch);
		}
		saleListModel = ListModelParamUtil();
		ListModel<Map> saleItemLitModel = saleService.getSaleItemListMdelByConditions(jsonObject,saleListModel);
	    jsonObject.clear();
		jsonObject.put(SaleConstantEnum.TOTALPROPERTY.getType(), saleItemLitModel.getTotal());
	    jsonObject.put(SaleConstantEnum.ROOT.getType(), saleItemLitModel.getResult());
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	public String fillExpressItemBrowser(){
		jsonObject = new HashMap();
		if(queryParam != null){
			JSONObject queryObject = JSONObject.fromObject(queryParam);
			jsonObject = (Map) JSONObject.toBean(queryObject, Map.class);	
		}
		if(searchJson!=null){
			PartPropertySearch partPropertySearch=JsonUtil.getBean(searchJson, PartPropertySearch.class);
			jsonObject.put("searchJson", partPropertySearch);
		}
		saleListModel = ListModelParamUtil();
		ListModel<Map> saleItemLitModel = saleService.getExpressItemListMdelByConditions(jsonObject,saleListModel);
	    jsonObject.clear();
		jsonObject.put(SaleConstantEnum.TOTALPROPERTY.getType(), saleItemLitModel.getTotal());
	    jsonObject.put(SaleConstantEnum.ROOT.getType(), saleItemLitModel.getResult());
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	private ListModel ListModelParamUtil() {
		saleListModel = new ListModel();
		saleListModel.setPage(start ==null && limit == null ? 1: start / limit + 1);
		saleListModel.setPageSize(limit == null ? Integer.valueOf(ConstantParamEnum.PAGESIZE.getType()) : limit);
		saleListModel.setOrderBy(sort);
		if (dir != null){
			saleListModel.setDesc(dir.equals(ConstantParamEnum.DESC.getType()) ? true : false);
		}else{
			saleListModel.setDesc(true);
		}	
		return saleListModel;
	}

	public Map<String, Object> getJsonObject() {
		return jsonObject;
	}

	public void setJsonObject(Map<String, Object> jsonObject) {
		this.jsonObject = jsonObject;
	}

	public String getQueryParam() {
		return queryParam;
	}

	public void setQueryParam(String queryParam) {
		this.queryParam = queryParam;
	}

	@SuppressWarnings("unchecked")
	public ListModel getSaleListModel() {
		return saleListModel;
	}

	@SuppressWarnings("unchecked")
	public void setSaleListModel(ListModel saleListModel) {
		this.saleListModel = saleListModel;
	}

	public SaleService getSaleService() {
		return saleService;
	}

	public void setSaleService(SaleService saleService) {
		this.saleService = saleService;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getLimit() {
		return limit;
	}

	public void setLimit(Integer limit) {
		this.limit = limit;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getDir() {
		return dir;
	}

	public void setDir(String dir) {
		this.dir = dir;
	}


	public void setSearchJson(String searchJson) {
		this.searchJson = searchJson;
	}	
	
}
